import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:image_pickers/image_pickers.dart';
import 'package:shop/common/common.dart';

class MinePage extends StatefulWidget {
  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {
  File? _image;

  void _photo(BuildContext context) async {
    List<Media> listImagePaths = await ImagePickers.pickerPaths(
      galleryMode: GalleryMode.image,
      selectCount: 1,
      showCamera: true,
      cropConfig: CropConfig(enableCrop: true, height: 1, width: 1),
    );
    if (listImagePaths.isNotEmpty) {
      setState(() {
        _image = File(listImagePaths.first.path!);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("我的"), centerTitle: true, leading: SizedBox(), actions: [
        IconButton(
          icon: Icon(
            Icons.settings,
            color: Colors.white,
          ),
          onPressed: () {
            EasyLoading.showToast("设置");
          },
        )
      ]),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(
            height: 30,
          ),
          GestureDetector(
            onTap: () => _photo(context),
            child: PhysicalModel(
              color: Colors.white,
              shape: BoxShape.circle,
              clipBehavior: Clip.antiAlias,
              child: _image == null
                  ? CachedNetworkImage(
                      imageUrl: 'https://pic.netbian.com/uploads/allimg/171116/215718-1510840638a4b6.jpg',
                      height: 70,
                      width: 70,
                      fit: BoxFit.cover,
                    )
                  : Image.file(
                      _image!,
                      width: 70,
                      height: 70,
                      fit: BoxFit.cover,
                    ),
            ),
          ),
          SizedBox(
            height: 8,
          ),
          Text(
            "开学到家了",
            style: AppFont.textStyle(14, color: Colors.white),
          ),
          Text(
            "ID:33597",
            style: AppFont.textStyle(11, color: AppColor.mainColor),
          ),
          SizedBox(
            height: 15,
          ),
          _Cell(
            title: "我的订单",
            onTap: () => EasyLoading.showToast("我的订单"),
          ),
          _Cell(
            title: "公告列表",
            onTap: () => EasyLoading.showToast("公告列表"),
          ),
          _Cell(
            title: "客服",
            onTap: () => EasyLoading.showToast("暂未开放"),
          ),
          _Cell(
            title: "关于我们",
            onTap: () => EasyLoading.showToast("暂未开放"),
          ),
        ],
      ),
    );
  }
}

class _Cell extends StatelessWidget {
  final String title;
  final Function()? onTap;

  const _Cell({Key? key, required this.title, this.onTap}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 13),
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 15),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    title,
                    style: AppFont.textStyle(14, color: Colors.black),
                  ),
                  Icon(
                    Icons.arrow_forward_ios_outlined,
                    size: 13,
                    color: Colors.grey,
                  )
                ],
              ),
            ),
            Divider(
              height: 1,
              color: const Color(0xff1E2328),
            )
          ],
        ),
      ),
    );
  }
}
